/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mwt.xml.xdbforms.xformlayer.transactions.impl;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * Progetto Master Web Technology
 * @author Gianfranco Murador
 * Copyright (C) 2009
 */
public class XMLMerger {

    public static Document merge(String expression, Document base, Document merge)
            throws Exception {
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();
        XPathExpression compiledExpression = xpath.compile(expression);
        return merge(compiledExpression, base, merge);
    }

    public static Document merge(XPathExpression expression, Document base, Document merge)
            throws Exception {

       
        Node impRoot = base.importNode(merge.getDocumentElement(), true);
        Node nodeApp =(Node) expression.evaluate(base, XPathConstants.NODE);

        System.out.println("nodeApp "+nodeApp);
        System.out.println("impRoot "+impRoot);

        nodeApp.appendChild(impRoot);
        return base;
     
    }

 
}
